package app.sevenc.lib_framework

import android.content.Context
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.multidex.MultiDexApplication
import com.blankj.utilcode.util.ProcessUtils
import com.blankj.utilcode.util.Utils

/**
 * BaseApp
 *
 * @author Seven-C
 * @date 2021/8/18
 */
abstract class BaseFrameApplication : MultiDexApplication(), ViewModelStoreOwner {

    private val mAppViewModelStore: ViewModelStore by lazy { ViewModelStore() }

    private val mFactory by lazy {
        ViewModelProvider.AndroidViewModelFactory.getInstance(this)
    }

    private val mViewModelProvider by lazy { ViewModelProvider(this, mFactory) }

    override fun onCreate() {
        super.onCreate()
        // 主进程初始化
        if (ProcessUtils.isMainProcess()) {
            initApp()
        }
    }

    private fun initApp() {
        Utils.init(this)
    }

    override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }

    /**
     * 获取一个全局的ViewModelProvider
     *
     * @return ViewModelProvider
     */
    fun getAppViewModelProvider(): ViewModelProvider {
        return mViewModelProvider
    }
}